/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;


import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.event.ListSelectionEvent;

import loc.messages;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.HighlighterFactory;

import sys.Video;
import sys.VideoTableModel;

/**
 *
 * @author Jonas
 */
public class VideoTable extends JXTable {
	private static VideoTableModel vtModel; 
    public VideoTable() {
        super();
        vtModel = new VideoTableModel();
        this.setModel(vtModel);
        this.getTableHeader().getColumnModel().getColumn(0).setHeaderValue(messages.getString("VideoTable.0")); //$NON-NLS-1$
        this.getTableHeader().getColumnModel().getColumn(1).setHeaderValue(messages.getString("VideoTable.1")); //$NON-NLS-1$
        this.getTableHeader().getColumnModel().getColumn(2).setHeaderValue(messages.getString("VideoTable.2")); //$NON-NLS-1$
        this.getTableHeader().getColumnModel().getColumn(3).setHeaderValue(messages.getString("VideoTable.3")); //$NON-NLS-1$
        this.getTableHeader().repaint();
        this.setRowHeight(25);
        this.setIntercellSpacing(new Dimension(10, 10));
        this.setSortable(false);
        this.setBorder(null);
//        this.setRolloverEnabled(true);
//        this.addHighlighter(
//        		HighlighterFactory.UIColorHighlighter(HighlightPredicate.ODD));
        		
//            new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW,
//    		  null,
//            null));
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        super.setValueAt(aValue, row, column);
    }

    public void updateProgressCellRenderer() {
        for (int i = 0; i < this.getTableHeader().getColumnModel().getColumnCount(); i++) {
            if(this.getTableHeader().getColumnModel().getColumn(i).getHeaderValue().equals(messages.getString("VideoTable.2"))) { //$NON-NLS-1$
                this.getColumnModel().getColumn(i).setCellRenderer(new TableProgressBar());
            }
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        super.valueChanged(e);
        if(this.getSelectedRows().length >= 1) {
            //TODO update textVideo* JTextFields
        }
    }
    
    public void addVideo(Video video) {
    	video.addDownloadProgressListener(((VideoTableModel)this.getModel()));
    	((VideoTableModel)this.getModel()).addVideo(video);
    	this.updateProgressCellRenderer();
    }
    
    public void removeVideo(Video video) {
    	((VideoTableModel)this.getModel()).removeVideo(video);
    	this.updateProgressCellRenderer();
    }
    
    public void removeVideoAtIndexes(int[] indexes) {
    	((VideoTableModel)this.getModel()).removeVideoAtIndexes(indexes);
    	this.updateProgressCellRenderer();
    }
        
    static void addVideos(List<Video> videos) {
    	for(Video v : videos) {
    		v.addDownloadProgressListener(vtModel);
    		vtModel.addVideo(v);
    	}
    }
    
    static List<Video> getVideos() {
    	return vtModel.getVideos();
    }
    
    static Video getVideo(int index) {
    	return vtModel.getVideo(index);
    }
    
}
